package com.emm.yixun.mobile.ui.calculateprice;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.base.BaseActivity;
import com.emm.yixun.mobile.model.GetCalculatePrice;

/**
 * 
 * <p>
 * <b>Title：</b>算价结果界面
 * </p>
 * <p>
 * <b>Description：</b>
 * </p>
 * 
 * @author tangxin
 * @date 2016年12月6日 下午4:11:43
 */
@SuppressLint("NewApi")
public class EndPriceActivity extends SwipeBackActivity {
	private static final String TAG = "EndPriceActivity";

	/** 标题 */
	TextView title_main;

	/** 返回按钮 */
	ImageView back_btn;

	/** 分享 */
	ImageView more_btn;

	LinearLayout linear_bean;

	ScrollView srcollview_;

	/** 等额本息还款: 贷款总额 */
	TextView loanTotal_view;

	/** 等额本息还款: 还款总额 */
	TextView repaymentTotal_view;

	/** 等额本息还款: 支付利息 */
	TextView paymentInterest_view;

	/** 等额本息还款: 贷款月数 */
	TextView loanMonth_view;

	/** 等额本息还款: 每月还款 */
	TextView monthlyRepayment_view;

	/** 等额本金还款: 贷款总额 */
	TextView loanTotal2_view;

	/** 等额本金还款: 还款总额 */
	TextView repaymentTotal2_view;

	/** 等额本金还款: 支付利息 */
	TextView paymentInterest2_view;

	/** 等额本金还款: 贷款月数 */
	TextView loanMonth2_view;

	/** 等额本金还款: 首月还款 */
	TextView firstMonthRepayment_view;

	/** 等额本金还款: 每月递减 */
	TextView monthlyDecline_view;

	/** 房源信息 */
	TextView pr_name;

	String pr_namestr = "";

	/** 客户名 */
	TextView customerName_view;

	String customerName = "";

	/** 联系电话 */
	TextView customerPhone_view;

	/**用户手机*/
	String customerPhone = "";

	TextView businessLoanRate_textview, fundLoanRate_textview;

	String businessLoanRate = "";

	String fundLoanRate = "";
	/** 1="公积金贷款" 2="商业贷款" 3="组合贷款" */
	int loantype_;

	TextView fundLoanRate_Prompt, businessLoanRate_Prompt;

	/** 折扣倍率 */
	String Rate = "";

	/** 侧滑关闭框架 */
	private SwipeBackLayout mSwipeBackLayout;

	@Override
	protected void onResume() {
		BaseActivity.activity = this; // TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.endprice_activity);

		SetSwipeBackLayout(mSwipeBackLayout);

		BaseActivity.activity = this;

		pr_namestr = getIntent().getStringExtra("pr_namestr");
		customerName = getIntent().getStringExtra("customerName");
		customerPhone = getIntent().getStringExtra("customerPhone");
		businessLoanRate = getIntent().getStringExtra("businessLoanRate");
		fundLoanRate = getIntent().getStringExtra("fundLoanRate");
		loantype_ = getIntent().getIntExtra("loantype_", 0);
		Rate = getIntent().getStringExtra("Rate");

		initView();

		SetRelativiLayoutPadding(R.id.main_head_top_rela);

		initBtn();
		SetViewDate();
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 给view设置显示值
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 下午2:12:43
	 */
	private void SetViewDate() {
		GetCalculatePrice getcalculate = EmmApplication.getcalculate;

		loanTotal_view.setText(getcalculate.getLoanTotal() + "万元");
		repaymentTotal_view.setText(getcalculate.getRepaymentTotal() + "万元");
		loanMonth_view.setText(getcalculate.getLoanMonth() + "月");
		monthlyRepayment_view.setText(getcalculate.getMonthlyRepayment() + "元");
		loanTotal2_view.setText(getcalculate.getLoanTotal2() + "万元");
		repaymentTotal2_view.setText(getcalculate.getRepaymentTotal2() + "万元");
		paymentInterest2_view.setText(getcalculate.getPaymentInterest2() + "万元");
		paymentInterest_view.setText(getcalculate.getPaymentInterest() + "万元");
		loanMonth2_view.setText(getcalculate.getLoanMonth2() + "月");
		firstMonthRepayment_view.setText(getcalculate.getFirstMonthRepayment() + "元");
		monthlyDecline_view.setText(getcalculate.getMonthlyDecline() + "元");
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 初始化view
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 下午2:13:02
	 */
	private void initView() {
		title_main = (TextView) findViewById(R.id.title_main);
		title_main.setText("计算结果");
		back_btn = (ImageView) findViewById(R.id.back_btn);
		more_btn = (ImageView) findViewById(R.id.more_btn);
		linear_bean = (LinearLayout) findViewById(R.id.linear_bean);
		srcollview_ = (ScrollView) findViewById(R.id.srcollview_);
		loanTotal_view = (TextView) findViewById(R.id.loanTotal_view);
		repaymentTotal_view = (TextView) findViewById(R.id.repaymentTotal_view);
		paymentInterest_view = (TextView) findViewById(R.id.paymentInterest_view);
		loanMonth_view = (TextView) findViewById(R.id.loanMonth_view);
		monthlyRepayment_view = (TextView) findViewById(R.id.monthlyRepayment_view);
		loanTotal2_view = (TextView) findViewById(R.id.loanTotal2_view);
		repaymentTotal2_view = (TextView) findViewById(R.id.repaymentTotal2_view);
		paymentInterest2_view = (TextView) findViewById(R.id.paymentInterest2_view);
		loanMonth2_view = (TextView) findViewById(R.id.loanMonth2_view);
		firstMonthRepayment_view = (TextView) findViewById(R.id.firstMonthRepayment_view);
		monthlyDecline_view = (TextView) findViewById(R.id.monthlyDecline_view);
		pr_name = (TextView) findViewById(R.id.pr_name);
		if (EmmApplication.isNull(pr_namestr)) {
			pr_name.setText(pr_namestr);
		} else {
			pr_name.setText("--");
		}
		customerName_view = (TextView) findViewById(R.id.customerName_view);
		if (EmmApplication.isNull(customerName)) {
			customerName_view.setText(customerName);
		} else {
			customerName_view.setText("--");
		}
		customerPhone_view = (TextView) findViewById(R.id.customerPhone_view);
		if (EmmApplication.isNull(customerPhone)) {
			customerPhone_view.setText(customerPhone);
		} else {
			customerPhone_view.setText("--");
		}
		businessLoanRate_textview = (TextView) findViewById(R.id.businessLoanRate_textview);
		fundLoanRate_textview = (TextView) findViewById(R.id.fundLoanRate_textview);
		fundLoanRate_Prompt = (TextView) findViewById(R.id.fundLoanRate_Prompt);
		businessLoanRate_Prompt = (TextView) findViewById(R.id.businessLoanRate_Prompt);

		String Rate1 = "";
		if (Double.valueOf(Rate).doubleValue() > 1) {
			Rate1 = "(" + Rate + "倍)";
		} else if (Double.valueOf(Rate).doubleValue() == 1) {
			Rate1 = "(1.00 倍)";
		} else {
			Rate1 = EmmApplication.replace("0", "",
					"(" + EmmApplication.drop2("" + (int) (Double.valueOf(Rate).doubleValue() * 100)) + "折)");
		}
		if (loantype_ == 1) {
			businessLoanRate_Prompt.setText("贷款利率：");
			businessLoanRate_textview.setText("");
			fundLoanRate_textview.setText(fundLoanRate + "%");
			fundLoanRate_Prompt.setText("公积金");
		} else if (loantype_ == 2) {
			businessLoanRate_Prompt.setText("贷款利率：商业");
			fundLoanRate_Prompt.setText(Rate1);
			businessLoanRate_textview.setText(businessLoanRate + "%");
			fundLoanRate_textview.setText("");
		} else if (loantype_ == 3) {
			businessLoanRate_Prompt.setText("贷款利率：商业");
			fundLoanRate_Prompt.setText(Rate1 + ",公积金");
			businessLoanRate_textview.setText(businessLoanRate + "%");
			fundLoanRate_textview.setText(fundLoanRate + "%");
		}
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 初始化监听事件
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 下午2:13:12
	 */
	private void initBtn() {
		back_btn.setVisibility(View.VISIBLE);
		back_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		more_btn.setVisibility(View.VISIBLE);
		more_btn.setImageResource(R.drawable.ico_share);
		more_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 截图分享
				Intent shareIntent = new Intent();
				shareIntent.setAction(Intent.ACTION_SEND);
				File f = getScrollViewBitmap(srcollview_);
				if (f != null && f.exists() && f.isFile()) {
					Uri u = Uri.fromFile(f);
					shareIntent.putExtra(Intent.EXTRA_STREAM, u);
				}
				shareIntent.setType("image/*");
				shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(Intent.createChooser(shareIntent, "分享到:"));
			}
		});

	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 截取scrollview
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @param scrollView
	 * @return
	 * @author tangxin
	 * @date 2016年12月7日 下午2:13:26
	 */
	public static File getScrollViewBitmap(ScrollView scrollView) {
		File picpath = null;
		try {
			picpath = File.createTempFile(EmmApplication.geTimes(), ".png", new File(EmmApplication.getDatePatch()));
		} catch (IOException e) {
			e.printStackTrace();
		}
		int h = 0;
		Bitmap bitmap;
		// 获取listView实际高度
		for (int i = 0; i < scrollView.getChildCount(); i++) {
			h += scrollView.getChildAt(i).getHeight();
		}
		Log.d(TAG, "实际高度:" + h);
		Log.d(TAG, " 高度:" + scrollView.getHeight());
		// 创建对应大小的bitmap
		bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.ARGB_8888);
		final Canvas canvas = new Canvas(bitmap);
		scrollView.draw(canvas);
		// 测试输出
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(picpath.toString());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		try {
			if (null != out) {
				bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
				out.flush();
				out.close();
			}
		} catch (IOException e) {
		}
		return picpath;
	}
}
